package cn.core.sign.rest;

import cn.core.sign.eo.UsrRole;
import cn.core.sign.qo.UsrRoleQO;
import cn.core.sign.service.UsrRoleService;
import cn.core.sign.vo.UsrRoleVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户角色 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("sign/usrRole")
public class UsrRoleController extends CoreController {

  public static final String name = "用户角色";

  @Resource
  private UsrRoleService usrRoleService;

  /**
   * 用户角色 >>> 保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(save)
  public Res<UsrRole> save(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.save(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 批量保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(saveBatch)
  public Res<UsrRole> saveBatch(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.saveBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(update)
  public Res<UsrRole> update(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.update(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 批量修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(updateBatch)
  public Res<UsrRole> updateBatch(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.updateBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(remove)
  public Res<UsrRole> remove(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      usrRoleService.remove(param.get());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 批量删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(removeBatch)
  public Res<UsrRole> removeBatch(@RequestBody Param<UsrRole> param) throws Exception {
    Res<UsrRole> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      usrRoleService.removeBatch(param.getData());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(count)
  public Res<UsrRole> count(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRole> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(usrRoleService.count(null));
      } else {
        res.setCount(usrRoleService.count(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pk)
  public Res<UsrRole> pk(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRole> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.pk(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(query)
  public Res<UsrRole> query(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRole> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(usrRoleService.query(null));
      } else {
        res.setData(usrRoleService.query(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(paging)
  public Res<UsrRole> paging(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRole> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.paging(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 业务数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(countVo)
  public Res<UsrRoleVO> countVo(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRoleVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(usrRoleService.countVo(null));
      } else {
        res.setCount(usrRoleService.countVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 业务主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pkVo)
  public Res<UsrRoleVO> pkVo(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRoleVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.pkVo(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 业务查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(queryVo)
  public Res<UsrRoleVO> queryVo(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRoleVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(usrRoleService.queryVo(null));
      } else {
        res.setData(usrRoleService.queryVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 用户角色 >>> 业务分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(pagingVo)
  public Res<UsrRoleVO> pagingVo(@RequestBody Param<UsrRoleQO> param) throws Exception {
    Res<UsrRoleVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(usrRoleService.pagingVo(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
